import { Elysia, t } from 'elysia'
import { swagger } from '@elysiajs/swagger'
import { cors } from '@elysiajs/cors'
import { user } from "./apps/user"
import { post } from "./apps/post"
import { task } from "./apps/task"


// const app = new Elysia()
const app = new Elysia()
  // 全局中间件
  .onBeforeHandle(() => {
    console.log('onBeforeHandle')
  })
  // 接口文档
  .use(swagger({
    path:"/api/swagger",
    documentation: {
      info: {
        title: 'Elysia Documentation',
        version: '1.0.0'
      }
    }
  }))
  // 跨域配置
  .use(cors())
  .use(user)
  .use(post)
  // .use(task)
  // 心跳检测
  .get('/api/', () => 'ok')
  // 其他接口
  // 文件上传接口
  .post('/api/upload', ({ body }) => body.file, {
    body: t.Object({
      file: t.File({
        type: 'image'
      })
    })
  })
  // Route-specific middleware
  // 单个路由的接口鉴权
  .get('/api/protected', () => 'protected', {
    beforeHandle({ status, headers }) {
      console.log(headers.authorizaton)
      if (!headers.authorizaton)
        return status(401)
    }
  })
  // .listen(3000)
  .listen(process.env.PORT ?? 3000)


console.log(
  `Elysia is running at ${app.server?.hostname}:${app.server?.port}`
);
